package com.mmg.cosplay.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.jwt.StpLogicJwtForStateless;
import cn.dev33.satoken.stp.StpLogic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file.upload.path}")
    String realPaths;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        //配置server虚拟路径，handler为前台访问的目录，locations为files相对应的本地路径
        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + realPaths);
    }

    /**
     * 注册Sa-Token 的拦截器，打开注解式鉴权功能
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("doc.html")
                .excludePathPatterns("/doc.html/**")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/upload/**")
                .excludePathPatterns("/common/upload")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/captcha/**")
                .excludePathPatterns("/user/logout");
    }

    /**
     * 添加自定义的消息转换器
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建Jackson消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        // 设置ObjectMapper,实现自定义的Jackson配置
        converter.setObjectMapper(new JacksonObjectMapper());
        // 将自定义的Jackson消息转换器添加到转换器列表中
        converters.add(1, converter);
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        // 允许跨域的源
        config.addAllowedOrigin("*");

        // 允许跨域的请求头
        config.addAllowedHeader("*");

        // 允许跨域的方法
        config.addAllowedMethod("*");

        // 允许携带cookie等认证信息
        // config.setAllowCredentials(true);

        // 设置跨域请求的有效期
        config.setMaxAge(3600L);

        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

    @Bean
    public StpLogic getStpLogicJwt() {
        //  Sa-Token 整合 jwt (Stateless 无状态模式)
        return new StpLogicJwtForStateless();
    }
}
